<?php
declare (strict_types = 1);

namespace app\controller;

use app\middleware\Auth as AuthMiddleware;
use think\Request;
use app\validate\User as UserValidate;
class Hostadv
{

    protected $middleware = [AuthMiddleware::class];
    private $toast = 'public/toast';
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create(Request $request)
    {
        //
        $data = $request->param();
        return view('create',[
            'hostcat_id'    => $data['hostcat_id']
        ]);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
        $data = $request->param();
        try {
            validate(UserValidate::class)->scene('adv')->batch(true)->check($data);
        } catch (ValidateException $exception) {
            return view($this->toast, [
                'infos'     =>  $exception->getError(),
                'url_text'  =>  '返回上一页',
                'url_path'  =>  url('/hostadv/create')."?hostcat_id=".$data['hostcat_id']
            ]);
        }
        //写入数据库
        $id = \app\model\Hostadv::create($data)->getData('id');
        //返回
        return $id ? view($this->toast, [
            'infos'     =>  ['添加成功~'],
            'url_text'  =>  '返回列表页',
            'url_path'  =>  url('/hostadv/'.$data['hostcat_id'])
        ]) : '添加失败';
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
        $data=\app\model\Hostcat::hasWhere('hostadv',['hostcat_id'=>$id])->with('hostadv')->select();
        return view('index', [
            'list'  => $data->toArray(),
            'hostcat_id'=> $id,
        ]);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit(Request $request,$id)
    {
        //
        $data = $request->param();
        return view('edit', [
            'obj'   =>  \app\model\Hostadv::find($id),
            'hostcat_id'=> $data['hostcat_id'],
        ]);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
        $data = $request->param();
        if (!$data['img']){
            $data['img'] = \app\model\Hostadv::where('id',$data['id'])->column('img');
        }
        try {
            validate(UserValidate::class)
                ->scene('adv')
                ->batch(true)
                ->check($data);
        } catch (ValidateException $exception) {
            return view($this->toast, [
                'infos'     =>  $exception->getError(),
                'url_text'  =>  '返回上一页',
                'url_path'  =>  url('/hostadv/'.$id.'/edit'.'?hostcat_id='.$data['hostcat_id'])
            ]);
        }

        return \app\model\Hostadv::update($data) ? view($this->toast, [
            'infos'     =>  ['恭喜，修改成功~'],
            'url_text'  =>  '返回列表页',
            'url_path'  =>  url('/hostadv/'.$data['hostcat_id'])
        ]) : '修改失败';

    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete(Request $request,$id)
    {
        //
        $data = $request->param();
        if(!$request->has('hostcat_id','get')){

        }
        return \app\model\Hostadv::destroy($id) ? view($this->toast, [
            'infos'     =>  ['恭喜，删除成功~'],
            'url_text'  =>  '返回列表页',
            'url_path'  =>  url('/hostadv/'.$data['hostcat_id'])
        ]) : '删除失败！';
    }
}
